package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.annotation.UserParams;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LoginException;
import cn.wolfcode.wolf2w.redis.service.IStrategyVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 甘静斌 on 2020-08-07 14:13
 */
@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyVORedisService strategyVORedisService;

    @GetMapping("/userInfo")
    public Object get(String id){

        return userInfoService.get(id);

    }

    /**
     * 判断用户输入的手机号是否存在
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){

        boolean userInfo =  userInfoService.checkPhone(phone);

        return userInfo;

    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){

        userInfoService.sendVerifyCode(phone);

        return JsonResult.success();

    }

    //注册操作
    @PostMapping("/regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){

        /*try {*/
            //注册需要手机号,昵称,密码,验证码 校验,传到service层进行业务处理
            userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
       /* } catch (LoginException e) {
            e.printStackTrace();
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
        }catch (Exception ex){

            ex.printStackTrace();
            return JsonResult.defaultError();
        }*/

            return JsonResult.success();

    }

    //登录操作
    @PostMapping ("/login")
    public Object login(String username,String password){

        //获取user
        UserInfo user = userInfoService.findByUsernameAndPassword(username,password);
        //获取token
        String token = userInfoRedisService.getToken(user);

        //将token跟user对象使用json格式返回浏览器
        HashMap<Object, Object> map = new HashMap<>();
        map.put("user",user );
        map.put("token",token );
        return JsonResult.success(map);

    }

    //第二次访问接口,用户不能每次过30分钟就要登陆一次
    @GetMapping("/currentUser")
    @LoginAnnotation
    public Object currentUser(HttpServletRequest request){

        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        return JsonResult.success(userInfo);

    }

    /**
     * 收藏回显 + 1,星星变蓝,并提示
     * @param userId
     * @return
     */

    @GetMapping("/strategies/favor")
    public Object favor(String userId){

        List<String> sids = strategyVORedisService.queryByFavor(userId);

        return JsonResult.success(sids);
    }
}
